package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.atguigu.gmall.product.service.BaseSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SPH_Demo01
 * 2022/11/1 19 08
 */
@RestController
@Api
@RequestMapping ("/admin/product")
public class AttrController {
    @Autowired
    BaseAttrValueService baseAttrValueService;

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseSaleAttrService baseSaleAttrService;

    /**
     * 通过c1id c2id c3id 查询产品信息
     *
     * @param c1id:第一大类Id
     * @param c2id:第一大类下的子类Id
     * @param c3id:第一大类下的子类的子类id，最底层
     * @return :产品列表信息
     */
    @GetMapping ("/attrInfoList/{c1id}/{c2id}/{c3id}")
    public Result attrInfoList (@PathVariable Long c1id,
                                @PathVariable Long c2id,
                                @PathVariable Long c3id) {
        List<BaseAttrInfo> list = this.baseAttrInfoService.getAttrInfoByCategoryId (c1id, c2id, c3id);
        return Result.ok (list);
    }

    /**
     * 添加/修改 产品信息：平台目录
     *
     * @return
     * @param: baseAttrInfo
     */
    @PostMapping ("/saveAttrInfo")
    public Result saveAttrInfo (@RequestBody BaseAttrInfo baseAttrInfo) {
        this.baseAttrInfoService.deleteAadSaveAndUpdateBaseAttrInfoByBaseAttrInfo (baseAttrInfo);

        return Result.ok ();
    }


    /**
     * 查询产品平台属性信息
     *
     * @param attrId
     * @return
     */
    @GetMapping ("/getAttrValueList/{attrId}")
    public Result getAttrValueList (@PathVariable Long attrId) {
        QueryWrapper<BaseAttrValue> baseAttrValueQueryWrapper = new QueryWrapper<> ();
        baseAttrValueQueryWrapper.eq ("attr_id", attrId);
        List<BaseAttrValue> baseAttrValues = this.baseAttrValueService.list (baseAttrValueQueryWrapper);
        return Result.ok (baseAttrValues);
    }

    //http://127.0.0.1/admin/product/baseSaleAttrList

    /**
     * 查询所有
     *
     * @return: list->销售属性
     */
    @GetMapping ("/baseSaleAttrList")
    public Result getSaleAttrList () {
        List<BaseSaleAttr> list = this.baseSaleAttrService.list ();
        return Result.ok (list);
    }


}
